//
//  FLLRecommendTalentViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/19.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLRecommendTalentViewController.h"
#import "FLLMacro.h"
#import "FLLRecommenTalentTableViewCell.h"
#import "FLLRecommendTalentModel.h"
#import "RecommendTalentData.h"
#import "RecommendTalentPost.h"
#import "RecommendTalentSecData.h"
#import "FLLWorksDetailViewController.h"

@interface FLLRecommendTalentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *bodyString;
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, assign) BOOL isLoading;

@property (nonatomic, strong) FLLRecommendTalentModel *dataModel;
@property (nonatomic, strong) NSMutableArray<RecommendTalentSecData *> *tableData;
@property (nonatomic, strong) NSMutableArray<RecommendTalentPost *> *collectionData;
@end

@implementation FLLRecommendTalentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createPOSTRequest];
    [self createTableView];
    
}
- (void)createTableView
{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"FLLRecommenTalentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"recommenTalentTableCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isLoading = NO;
        [self createPOSTRequest];
        [self.tableView.mj_header endRefreshing];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isLoading = YES;
        [self createPOSTRequest];
        [self.tableView.mj_footer endRefreshing];
    }];

   
}
- (void)createPOSTRequest
{
    
    // 起始或者刷新
    if (_isLoading == NO) {
        _pageCount = 1;
        
        // 加载
    } else {
        _pageCount++;
        
    }

    self.bodyString = [NSString stringWithFormat:@"page=%zd&methodName=TalentRecommend&size=20&version=4.40&", _pageCount];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:_bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.dataModel = [FLLRecommendTalentModel modelWithDic:result];
        // 起始或刷新
        if (_isLoading == NO) {
            [self.tableData removeAllObjects];
            self.tableData = (NSMutableArray *)self.dataModel.data.data;
        } else {
            [self.tableData addObjectsFromArray:(NSMutableArray *)self.dataModel.data.data];
        }
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.tableData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLRecommenTalentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommenTalentTableCell"];
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:_tableData[indexPath.row].head_img] placeholderImage:[UIImage imageNamed:@"userHeadImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.userName.font = [UIFont systemFontOfSize:15 * kWidthScale];
    cell.contentLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    cell.contentLabel.textColor = [UIColor lightGrayColor];
    cell.attentionButton.backgroundColor = FLLOrangeColor;
    cell.attentionButton.titleLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    
    cell.userName.text = self.tableData[indexPath.row].nick;
    cell.contentLabel.text = self.tableData[indexPath.row].declaration;
    cell.specialIcon.image = [UIImage imageNamed:@"达人"];
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    self.collectionData = (NSMutableArray *)self.tableData[indexPath.row].post;
    cell.collectionData = self.collectionData;
    
    cell.idBlock = ^(NSString *idString){
    
        FLLWorksDetailViewController *detailVC = [[FLLWorksDetailViewController alloc] init];
        detailVC.idString = idString;
        [self.navigationController pushViewController:detailVC animated:YES];
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return SCREEN_WIDTH * (2 * 10 / 375.0 + 1 / 6.0 + 1 / 3.0 );
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
